﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpCrack.Application.ConsoleApplication
{
	internal class ByteArrayHelper
	{
		/// <summary>
		/// Converts a hex formatted string to a byte array.
		/// </summary>
		/// <param name="hex"></param>
		/// <returns></returns>
		public static byte[] ConvertFromHexString(string hex)
		{
			if ((hex.Length & 1) != 0)
				hex = '0' + hex;

			byte[] result = new byte[hex.Length / 2];

			for (int i = 0; i < hex.Length; i += 2)
			{
				result[i / 2] = Byte.Parse(
					hex.Substring(i, 2), System.Globalization.NumberStyles.HexNumber
				);
			}

			return result;
		}

		/// <summary>
		/// Converts a byte arrays to a hex formatted string.
		/// </summary>
		/// <param name="p"></param>
		/// <returns></returns>
		public static string ConvertToHexString(byte[] hash)
		{
			StringBuilder sb = new StringBuilder(2 * hash.Length);

			foreach (byte b in hash)
				sb.Append(b.ToString("X2"));

			return sb.ToString();
		}
	}
}
